

#include <QtOpenGL/QtOpenGL>
#include "SceneEditorView.h"

#include "QGridGraphicsItem.h"


SceneEditorView::SceneEditorView(QWidget *parent)
    : QGraphicsView(parent)
{
#ifndef QT_NO_OPENGL
    setViewport(new QGLWidget());
#endif

    setMouseTracking(true);
}

SceneEditorView::~SceneEditorView()
{

}

void SceneEditorView::init(QGraphicsScene *scene)
{
    setScene(scene);

//    QImage image("D:\\Users\\aaronwang\\Private\\Workspace\\private\\game\\XYOnline\\resource\\Demo\\Scene\\Image\\1381.map.png");
//    QPixmap pixmapBkgnd = QPixmap::fromImage(image);
//    int w = pixmapBkgnd.width();
//    int h = pixmapBkgnd.height();
//    QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmapBkgnd);
//    scene->addItem(item);
//    scene->setSceneRect(0.0f, 0.0f, pixmapBkgnd.width(), pixmapBkgnd.height());

//    QPixmap pixmapMask("D:\\Users\\aaronwang\\Private\\Workspace\\private\\game\\XYOnline\\resource\\Demo\\Scene\\Image\\1124.map.mask\\8_0.png");
//    item = new QGraphicsPixmapItem(pixmapMask, item);
//    scene->addItem(item);
//    item->setPos(185.0f, 346.0f);
//    item->setFlag(QGraphicsItem::ItemIsMovable, true);
//    item->setFlag(QGraphicsItem::ItemIsFocusable, true);
//    item->setFlag(QGraphicsItem::ItemIsSelectable, true);

    QGridGraphicsItem *gridItem = new QGridGraphicsItem(scene);

}

void SceneEditorView::onImageInserted(ImageInfo *info, QGraphicsPixmapItem *item, int mode)
{
    QGraphicsScene *s = scene();
    item->setFlag(QGraphicsItem::ItemIsMovable, true);
    item->setFlag(QGraphicsItem::ItemIsFocusable, true);
    item->setFlag(QGraphicsItem::ItemIsSelectable, true);
    s->addItem(item);
}

void SceneEditorView::onImageDeleted(int index, qint64 cacheKey)
{
    QGraphicsScene *s = scene();
    QList<QGraphicsItem*> items = s->items();

    bool found = false;
    QGraphicsPixmapItem *item = 0;
    QList<QGraphicsItem*>::iterator itr = items.begin();
    while (itr != items.end())
    {
        item = static_cast<QGraphicsPixmapItem*>(*itr);
        if (item->pixmap().cacheKey() == cacheKey)
        {
            found = true;
            break;
        }

        ++itr;
    }

    if (found)
    {
        s->removeItem(item);
    }
}

void SceneEditorView::onImagePositionAdjusted(int index, qint64 cacheKey, const QPoint &pos)
{

}

void SceneEditorView::onImageModeModified(int index, qint64 cacheKey, int mode)
{

}

void SceneEditorView::onImageOrderSwaped(int srcIdx, int dstIdx, qint64 srcKey, qint64 dstKey)
{

}

